/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmLotacao.java
 *
 * Created on Nov 13, 2009, 11:30:03 PM
 */

package br.com.digitalpoint.form;

import br.com.digitalpoint.Funcionario;
import br.com.digitalpoint.Lotacao;
import br.com.digitalpoint.Setor;
import br.com.digitalpoint.dao.FuncionarioDAO;
import br.com.digitalpoint.dao.LotacaoDAO;
import br.com.digitalpoint.dao.SetorDAO;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author rodrigo
 */
public class FrmLotacao extends javax.swing.JFrame {

    private ArrayList<Setor> listaSetor;

    public void habilitarBotao(JButton j)
    {
        j.setEnabled(true);
    }

    public void desabilitarBotao(JButton j)
    {
        j.setEnabled(false);
    }

    public void carregaComboSetores()
    {
       try
       {
          listaSetor = new SetorDAO().consultarTodosSetores();
          cbxSetor.removeAllItems();
          cbxSetor.addItem("0-Não lotado ainda");
          cbxSetor.setSelectedItem("0-Não lotado ainda");
          for (int i = 0; i <listaSetor.size(); i ++)
          {
              //obtem um objeto Disciplina da lista’
              Setor d = listaSetor.get(i);
              //obtem o codigo
              int cod_setor = d.getCod_setor();
              //obtem o nome
              String nome = cod_setor+"-"+d.getNome();
              //adiciona o nome no combobox
              cbxSetor.addItem(nome);
          }
          cbxSetor.setEnabled(true);
        }
       catch (Exception e)
       {
            JOptionPane.showMessageDialog(null, "ERRO: "+e.getMessage());
       }
    }  

    /** Creates new form FrmLotacao */
    public FrmLotacao() {
        initComponents();
        carregaComboSetores();
        txtNome.setEnabled(false);
        desabilitarBotao(btnAlterar);
        desabilitarBotao(btnExcluir);
        desabilitarBotao(btnInserir);
    }

    private void limparCampos()
    {
        txtCodigo.setText(null);
        txtNome.setText(null);
        txtMatriculaConsulta.setText(null);
        cbxSetor.setSelectedIndex(0);
        desabilitarBotao(btnAlterar);
        desabilitarBotao(btnExcluir);
        desabilitarBotao(btnInserir);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTitulo = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        pnlConsulta = new javax.swing.JPanel();
        lblConsultaMatricula = new javax.swing.JLabel();
        txtMatriculaConsulta = new javax.swing.JTextField();
        btnConsultar = new javax.swing.JButton();
        pnlLotacao = new javax.swing.JPanel();
        lblCodigo = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        lblNome = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        btnLimpar = new javax.swing.JButton();
        btnInserir = new javax.swing.JButton();
        lblSetor = new javax.swing.JLabel();
        cbxSetor = new javax.swing.JComboBox();
        btnAlterar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnSetores = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("DIGITALPOINT - LOTAÇÕES");
        setResizable(false);

        lblTitulo.setText("DIGITAL POINT - LOTAÇÕES");

        pnlConsulta.setBorder(javax.swing.BorderFactory.createTitledBorder("Consulta"));

        lblConsultaMatricula.setText("Matrícula:");

        btnConsultar.setText("Consultar");
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout pnlConsultaLayout = new org.jdesktop.layout.GroupLayout(pnlConsulta);
        pnlConsulta.setLayout(pnlConsultaLayout);
        pnlConsultaLayout.setHorizontalGroup(
            pnlConsultaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlConsultaLayout.createSequentialGroup()
                .addContainerGap()
                .add(lblConsultaMatricula)
                .add(18, 18, 18)
                .add(txtMatriculaConsulta, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 68, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(27, 27, 27)
                .add(btnConsultar)
                .addContainerGap(27, Short.MAX_VALUE))
        );
        pnlConsultaLayout.setVerticalGroup(
            pnlConsultaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlConsultaLayout.createSequentialGroup()
                .addContainerGap()
                .add(pnlConsultaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblConsultaMatricula)
                    .add(txtMatriculaConsulta, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnConsultar))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlLotacao.setBorder(javax.swing.BorderFactory.createTitledBorder("Informações"));
        pnlLotacao.setToolTipText("");

        lblCodigo.setText("Código:");

        txtCodigo.setEditable(false);

        lblNome.setText("Nome:");

        btnLimpar.setText("Limpar");
        btnLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimparActionPerformed(evt);
            }
        });

        btnInserir.setText("Inserir");
        btnInserir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInserirActionPerformed(evt);
            }
        });

        lblSetor.setText("Setor:");

        cbxSetor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxSetorActionPerformed(evt);
            }
        });

        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        btnSetores.setText("Setores");
        btnSetores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetoresActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout pnlLotacaoLayout = new org.jdesktop.layout.GroupLayout(pnlLotacao);
        pnlLotacao.setLayout(pnlLotacaoLayout);
        pnlLotacaoLayout.setHorizontalGroup(
            pnlLotacaoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlLotacaoLayout.createSequentialGroup()
                .addContainerGap()
                .add(pnlLotacaoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pnlLotacaoLayout.createSequentialGroup()
                        .add(lblCodigo)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(txtCodigo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(pnlLotacaoLayout.createSequentialGroup()
                        .add(btnLimpar)
                        .add(18, 18, 18)
                        .add(btnInserir)
                        .add(14, 14, 14)
                        .add(btnAlterar)
                        .add(18, 18, 18)
                        .add(btnExcluir)
                        .add(18, 18, 18)
                        .add(btnSetores))
                    .add(pnlLotacaoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, pnlLotacaoLayout.createSequentialGroup()
                            .add(lblNome)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(txtNome))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, pnlLotacaoLayout.createSequentialGroup()
                            .add(lblSetor)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(cbxSetor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 263, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(95, Short.MAX_VALUE))
        );
        pnlLotacaoLayout.setVerticalGroup(
            pnlLotacaoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlLotacaoLayout.createSequentialGroup()
                .addContainerGap()
                .add(pnlLotacaoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblCodigo)
                    .add(txtCodigo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(26, 26, 26)
                .add(pnlLotacaoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblNome)
                    .add(txtNome, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(27, 27, 27)
                .add(pnlLotacaoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblSetor)
                    .add(cbxSetor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(153, 153, 153)
                .add(pnlLotacaoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnLimpar)
                    .add(btnAlterar)
                    .add(btnInserir)
                    .add(btnExcluir)
                    .add(btnSetores))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(10, 10, 10)
                        .add(lblTitulo))
                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 518, Short.MAX_VALUE)
                    .add(pnlLotacao, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(pnlConsulta, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(lblTitulo)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(pnlConsulta, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(pnlLotacao, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSetoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetoresActionPerformed
        // TODO add your handling code here:
        super.show(false);
        FrmSetor j = new FrmSetor();
        j.setLocationRelativeTo(null);
        j.show();        
}//GEN-LAST:event_btnSetoresActionPerformed


    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        try
        {
            FuncionarioDAO funcdao = new FuncionarioDAO();
            Funcionario func = new Funcionario();
            func = funcdao.consultarFuncionario(Integer.parseInt(txtMatriculaConsulta.getText()));
            txtNome.setText(func.getNome());
            txtCodigo.setText(String.valueOf(func.getMatricula()));

            SetorDAO setorDAO = new SetorDAO();
            String fim = setorDAO.consultarFuncionarioSetor(func.getMatricula());

            if(fim==null)
            {
                cbxSetor.setSelectedIndex(0);
                habilitarBotao(btnInserir);
                desabilitarBotao(btnAlterar);
                desabilitarBotao(btnExcluir);
            }
            else
            {
                cbxSetor.setSelectedItem(fim);
                habilitarBotao(btnAlterar);
                habilitarBotao(btnExcluir);
                desabilitarBotao(btnInserir);
            }   
                   
        } 
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Funcionário nao encontrado!\n", "MESSAGEM", 2);
            limparCampos();
        }
}//GEN-LAST:event_btnConsultarActionPerformed

    private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparActionPerformed
        limparCampos();
}//GEN-LAST:event_btnLimparActionPerformed

    private void btnInserirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInserirActionPerformed

        int matricula = Integer.parseInt(txtCodigo.getText());
        String cod_setor = (String)cbxSetor.getSelectedItem();
        String sub_cod_setor = cod_setor.substring(0,1);
        int sub_cod_setor1 = Integer.parseInt(sub_cod_setor);        

        //verificando preenchimento dos campos obrigatorios
        if((sub_cod_setor1!=0) && (matricula!=0))
        {
            //inserindo no banco
            Lotacao lotacao = new Lotacao(matricula,sub_cod_setor1);
            try
            {
                LotacaoDAO lotacaoDAO = new LotacaoDAO();
                lotacaoDAO.incluir(lotacao);
                JOptionPane.showMessageDialog(null, "Lotação incluida com sucesso.");
                limparCampos();
            }
            catch (Exception ex)
            {
                JOptionPane.showMessageDialog(null, "Não foi possivel incluir a Lotação.\n" + "ERRO: "+ex.getMessage(), "ERRO", 0 );
            }
        }
        else
        {
            //mensagem para falta de campos obrigatórios
            JOptionPane.showMessageDialog(null, "Você precisa preencher os campos obrigatórios!");
        }


       
    }//GEN-LAST:event_btnInserirActionPerformed

    private void cbxSetorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxSetorActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_cbxSetorActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        // TODO add your handling code here:
        int matricula = Integer.parseInt(txtCodigo.getText());
        String cod_setor = (String)cbxSetor.getSelectedItem();
        String sub_cod_setor = cod_setor.substring(0,1);
        int sub_cod_setor1 = Integer.parseInt(sub_cod_setor);

        //verificando preenchimento dos campos obrigatorios
        if((sub_cod_setor1!=0) && (matricula!=0))
        {
            //inserindo no banco
            Lotacao lotacao = new Lotacao(matricula,sub_cod_setor1);
            try
            {
                LotacaoDAO lotacaoDAO = new LotacaoDAO();
                lotacaoDAO.alterar(lotacao);
                JOptionPane.showMessageDialog(null, "Lotação alterada com sucesso.");
                limparCampos();
            }
            catch (Exception ex)
            {
                JOptionPane.showMessageDialog(null, "Não foi possivel alterar a Lotação.\n" + "ERRO: "+ex.getMessage(), "ERRO", 0 );
            }
        }
        else
        {
            //mensagem para falta de campos obrigatórios
            JOptionPane.showMessageDialog(null, "Você precisa preencher os campos obrigatórios!");
        }
}//GEN-LAST:event_btnAlterarActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        // TODO add your handling code here:
        try
        {
            int opcao = JOptionPane.showConfirmDialog(null, "Tem Certeza que deseja excluir essa Lotação?", "Confirmação de Exclusão", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            if(opcao == JOptionPane.YES_NO_OPTION)
            {
                LotacaoDAO lotacaoDAO = new LotacaoDAO();
                lotacaoDAO.excluir(Integer.parseInt(txtCodigo.getText()));
                JOptionPane.showMessageDialog(null, "Lotação excluída com sucesso!", "RESULTADO", 1);
                limparCampos();
                desabilitarBotao(btnAlterar);
                desabilitarBotao(btnExcluir);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "A exclusão foi cancelada!", "RESULTADO", 1);
            }
        }
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Não foi possivel excluir Lotação!\n" + "ERRO: " + ex.getMessage(), "ERRO", 0);
        }
}//GEN-LAST:event_btnExcluirActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmLotacao frmlotacao = new FrmLotacao();
                frmlotacao.setVisible(true);
                frmlotacao.setLocationRelativeTo(null);
                
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnConsultar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnInserir;
    private javax.swing.JButton btnLimpar;
    private javax.swing.JButton btnSetores;
    private javax.swing.JComboBox cbxSetor;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblConsultaMatricula;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblSetor;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JPanel pnlConsulta;
    private javax.swing.JPanel pnlLotacao;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtMatriculaConsulta;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables

}
